home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / ImageViewer.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  7.2 KB  |  287 lines

  1. package symantec.itools.multimedia;
  2.  
  3.  
  4. import java.awt.Canvas;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8. import java.awt.MediaTracker;
  9. import java.awt.Toolkit;
  10. import java.net.URL;
  11. import java.net.MalformedURLException;
  12.  
  13. //    01/29/97    TWB    Integrated changes from Windows
  14. //    02/19/97    RKM    Changed default for centerMode to correspond with DES file
  15. //                    Changed setCenterMode to call repaint instead of invalidate
  16. //                    Changed setCenterMode to repaint only if value had changed
  17.  
  18. /**
  19.  * ImageViewer component. Provides a platform-independent display of an Image.
  20.  * @version 1.0, Nov 26, 1996
  21.  * @author    Symantec
  22.  */
  23.  
  24.  
  25. public class ImageViewer
  26.     extends Canvas
  27. {
  28.     /**
  29.      * Image that this viewer is displaying.
  30.      */
  31.     protected Image image;
  32.  
  33.     /**
  34.      * Name of file, if any, associated with this image.
  35.      */
  36.     protected String fileName;
  37.  
  38.     /**
  39.      * URL of the image being displayed.
  40.      */
  41.     protected URL url;
  42.  
  43.     /**
  44.      * Center image within component display area.
  45.      */
  46.     protected boolean centerMode;
  47.  
  48.     /**
  49.      * Create default image viewer.
  50.      */
  51.     public ImageViewer()
  52.     {
  53.         image = null;
  54.         fileName = null;
  55.         url = null;
  56.         centerMode = true;
  57.     }
  58.  
  59.     /**
  60.      * Crate image viewer with filename.  The specified filename is used as
  61.      * the image source.
  62.      *
  63.      * @param str name of file containing the image source
  64.      * @exception MalformedURLException Thrown if URL cannot be generated from filename
  65.      *
  66.      */
  67.     public ImageViewer(String str)
  68.         throws MalformedURLException
  69.     {
  70.         setFileName(str);
  71.     }
  72.  
  73.     /**
  74.      * Create image viewer with URL.  The specified URL is used as
  75.      * the image source.
  76.      *
  77.      * @param url the URL of the image to be displayed
  78.      */
  79.     public ImageViewer(URL url)
  80.     {
  81.         setURL(url);
  82.     }
  83.  
  84.     /**
  85.      * Create image viewer with image.  The specified image is used as
  86.      * the image source
  87.      *
  88.      * @param img the image to be displayed
  89.      */
  90.     public ImageViewer(Image img)
  91.     {
  92.         setImage(img);
  93.     }
  94.  
  95.     /**
  96.      * Specify or change the image filename.
  97.      *
  98.      * @param str name of file containing image source
  99.      *
  100.      */
  101.     public void setFileName(String str)
  102.     {
  103.         try
  104.         {
  105.            fileName = str;
  106.            setURL(new URL(fileName));
  107.         }
  108.         catch(MalformedURLException e)
  109.         {
  110.             //System.out.println("malformed URL");
  111.         }
  112.         repaint();
  113.     }
  114.  
  115.     /**
  116.      * Obtain the filename associated with the current image.
  117.      *
  118.      * @return the name of the file, if any, associated with this image.  If
  119.                no file is associated with this image, returns null
  120.      */
  121.     public String getFileName()
  122.     {
  123.         return fileName;
  124.     }
  125.  
  126.     /**
  127.      * Specify or change the image URL.
  128.      *
  129.      * @param aUrl the URL of the image to be displayed
  130.      *
  131.      */
  132.     public void setURL(URL aUrl)
  133.     {
  134.         url = aUrl;
  135.         fileName = null;
  136.  
  137.         Image loadedImage = getToolkit().getImage(url);
  138.         if (loadedImage != null) {
  139.             setImage(loadedImage);
  140.             repaint();
  141.         }
  142.     }
  143.  
  144.    /**
  145.      * Obtain the URL associated with the current image.  If the image
  146.      * was specified by file name, or URL, it will have a URL which
  147.      * indicates its source.  Images created using the constructor
  148.      * with an Image parameter will have no associated URL.
  149.      *
  150.      * @return the name of the URL, if any, associated with this image.  If
  151.                no URL is associated with this image, returns null
  152.      */
  153.     public URL getURL()
  154.     {
  155.         return url;
  156.     }
  157.  
  158.     /**
  159.      * Specify or change the image centering mode.  Image may be centered, or
  160.      * drawn at the top left of the component area.
  161.      *
  162.      * @param flag center the image when true
  163.      */
  164.     public void setCenterMode(boolean flag)
  165.     {
  166.         if (centerMode != flag)
  167.         {
  168.             centerMode = flag;
  169.             repaint();
  170.         }
  171.     }
  172.  
  173.     /**
  174.      * Obtain current image centering mode.
  175.      *
  176.      * @return returns true if image is centered in component area
  177.      */
  178.     public boolean getCenterMode()
  179.     {
  180.         return centerMode;
  181.     }
  182.  
  183.     /**
  184.      * Set or change the current image.  Call this method if you want to
  185.      * specify directly the image to display.
  186.      *
  187.      * @param img the image to be displayed
  188.      */
  189.     public void setImage(Image img)
  190.     {
  191.         fileName = null;
  192.         image    = img;
  193.  
  194.         if (img != null) {
  195.             MediaTracker tracker;
  196.  
  197.             try
  198.             {
  199.                 tracker = new MediaTracker(this);
  200.                 tracker.addImage(image, 0);
  201.                 tracker.waitForID(0);
  202.             }
  203.             catch(InterruptedException e)
  204.             {
  205.             }
  206.         } else {
  207.             repaint();
  208.         }
  209.     }
  210.  
  211.     /**
  212.      * Obtain the image currently being displayed.
  213.      *
  214.      * @return the image currently displayed or null if no image
  215.      */
  216.     public Image getImage()
  217.     {
  218.         return image;
  219.     }
  220.  
  221.     /**
  222.      * Paints this component using the given graphics context.
  223.      * This is a standard Java AWT method which typically gets called
  224.      * by the AWT to handle painting this component. It paints this component
  225.      * using the given graphics context. The graphics context clipping region
  226.      * is set to the bounding rectangle of this component and its <0,0>
  227.      * coordinate is this component's top-left corner.
  228.      *
  229.      * The image is optionally centered depending on the centerMode flag.
  230.      *
  231.      * @param g the graphics context used for painting
  232.      * @see java.awt.Component#repaint
  233.      * @see java.awt.Component#update
  234.      */
  235.     public void paint(Graphics g)
  236.     {
  237.         if (image != null) {
  238.             Dimension dim = size();
  239.             g.clipRect(0, 0, dim.width, dim.height);
  240.  
  241.             int x = 0;
  242.             int y = 0;
  243.  
  244.             if (centerMode) {
  245.                 x += (dim.width - image.getWidth(this)) / 2;
  246.                 y += (dim.height - image.getHeight(this)) / 2;
  247.             }
  248.  
  249.             g.drawImage(image, x, y, this);
  250.         }
  251.     }
  252.  
  253.     /**
  254.      * Returns the recommended dimensions to properly display this component.
  255.      * This is a standard Java AWT method which gets called to determine
  256.      * the recommended size of this component.
  257.      *
  258.      * @return  If no image has been loaded, a dimension of 10 by 10 is returned.
  259.      *          If an image has been loaded, the height and width of the image
  260.      *          is returned.
  261.      *
  262.      * @see #minimumSize
  263.      */
  264.     public Dimension preferredSize()
  265.     {
  266.         if (image != null)
  267.             return (new Dimension(image.getWidth(this), image.getHeight(this)));
  268.         else
  269.             return new Dimension(10, 10);
  270.     }
  271.  
  272.     /**
  273.      * Returns the minimum dimensions to properly display this component.
  274.      * This is a standard Java AWT method which gets called to determine
  275.      * the minimum size of this component.
  276.      *
  277.      * @return  If no image has been loaded, a dimension of 10 by 10 is returned.
  278.      *          If an image has been loaded, the height and width of the image
  279.      *          is returned.
  280.      * @see #preferredSize
  281.      */
  282.     public Dimension minimumSize()
  283.     {
  284.         return preferredSize();
  285.     }
  286. }
  287.